<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">日期</label>
        <date-range-picker
          v-model="query.createTime"
          class="date-item"
        />
        <label class="el-form-item-label">名称</label>
        <el-input v-model="query.name" clearable placeholder="名称" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />

        <label class="el-form-item-label">类型</label>
        <el-select v-model="query.type" filterable clearable placeholder="请选择" style="width: 185px;" class="filter-item" @change="crud.toQuery">
          <el-option
            v-for="item in dict.summary_type"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <rrOperation :crud="crud" />
      </div>

      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="800px">
        <el-form ref="form" inline :model="form" :rules="rules" size="small" label-width="88px">
          <el-form-item label="日期" prop="date">
            <el-date-picker
              v-model="form.date"
              disabled
              type="date"
              placeholder="选择日期"
              style="width: 350px;"
            />
            <!-- <el-date-picker v-model="form.date" type="datetime" style="width: 370px;" /> -->
          </el-form-item>
          <el-form-item label="名称" prop="name">
            <el-input v-model="form.name" style="width: 250px;" />
          </el-form-item>
          <el-form-item label="类型" prop="type">
            <el-select v-model="form.type" filterable clearable placeholder="请选择" style="width: 250px;" class="filter-item">
              <el-option
                v-for="item in dict.summary_type"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="内容" prop="content">
            <div>
              <Editor ref="editor" class="eladmin-editor" style="width: 300px;" />
            </div>
            <!-- <el-input v-model="form.content" :rows="3" type="textarea" style="width: 370px;" /> -->
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <div class="card">
        <el-row :gutter="10">
          <el-col :xs="20" :sm="15" :md="15" :lg="18" :xl="18">
            <el-table ref="table" v-loading="crud.loading" class="card1" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
              <el-table-column type="selection" width="55" align="center" />
              <el-table-column prop="button" label="查看" align="center">
                <template slot-scope="scope">
                  <el-button type="primary" icon="el-icon-search" @click="getContent(scope.row)" />
                </template>
              </el-table-column>
              <el-table-column prop="name" label="名称" align="center" />
              <el-table-column prop="type" label="类型" align="center">
                <template slot-scope="scope">
                  {{ dict.label.summary_type[scope.row.type] }}
                </template>
              </el-table-column>

              <el-table-column prop="date" label="日期" align="center" />
              <!-- <el-table-column prop="createTime" label="创建日期" align="center" /> -->
              <el-table-column v-if="checkPer(['admin','summary:edit','summary:del'])" label="操作" width="150px" align="center">
                <template slot-scope="scope">
                  <udOperation
                    :data="scope.row"
                    :permission="permission"
                  />
                </template>
              </el-table-column>
            </el-table>
            <el-pagination
              :page-sizes="page.sizes"
              :page-size.sync="page.size"
              :total="page.total"
              :current-page.sync="page.page"
              style="margin-top: 8px;"
              layout="total, prev, pager, next, sizes"
              @size-change="crud.sizeChangeHandler($event)"
              @current-change="crud.pageChangeHandler"
            />
          </el-col>
          <el-col :xs="4" :sm="9" :md="9" :lg="6" :xl="6">
            <barq class="card2" style="width:100%;" />
          </el-col>
        </el-row></div>
      <el-dialog
        :before-close="summaryClose"
        :title="titleData"
        :visible.sync="handleState"
        width="700px"
        center
      >
        <span v-html="allContent" />
      </el-dialog>
    </div>
  </div>
</template>

<script>

import crudSummary from '@/api/summary'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import { pagination } from '@crud/crud'
import Editor from '@/views/components/MyEditor'
import DateRangePicker from '@/components/DateRangePicker'
import Barq from './barq.vue'

const defaultForm = { id: null, name: null, date: new Date(), createTime: null, delFlag: 0, content: null, type: '其他' }
export default {
  name: 'Summary',
  components: { crudOperation, rrOperation, udOperation, Editor, DateRangePicker, Barq },
  mixins: [presenter(), header(), form(defaultForm), crud(), pagination()],
  dicts: ['summary_type'],

  cruds() {
    return CRUD({ title: '日志管理', url: 'api/summary', idField: 'id', sort: ['date,desc', 'name,asc'], crudMethod: { ...crudSummary }

    })
  },
  data() {
    return {

      permission: {
        add: ['admin', 'summary:add'],
        edit: ['admin', 'summary:edit'],
        del: ['admin', 'summary:del']
      },
      rules: {
        name: [
          { required: true, message: '名称不能为空', trigger: 'blur' }
        ],
        date: [
          { required: true, message: '日期不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'name', display_name: '名称' }
      ],
      handleState: false,
      allContent: null,
      titleData: null

    }
  },
  methods: {

    /** 开始 "新建/编辑" - 之后 */
    [CRUD.HOOK.afterToCU](crud, form) {
      var that = this
      setTimeout(function() {
        // 初始化富文本编辑器
        that.$refs.editor.set(that.form.content)
        // 初始化文件列表
        // that.$refs.fileUpload.init(that.form.filePath, that.form.fileName)
      }, 0)
    },
    /** "新建/编辑" 验证 - 之前 */
    [CRUD.HOOK.beforeValidateCU]() {
      // 提交前获取富文本编辑器内数据
      this.crud.refresh()
      this.form.content = this.$refs.editor.get()

      // // 提交前获取文件列表路径与名称
      // if (this.$refs.fileUpload.upLoadForm.fileName) {
      //   this.form.fileName = this.$refs.fileUpload.upLoadForm.fileName
      //   this.form.filePath = this.$refs.fileUpload.upLoadForm.filePath
      // } else {
      //   this.form.fileName = ''
      //   this.form.filePath = ''
      // }
    },
    getContent(data) {
      this.allContent = data.content
      this.handleState = true
      this.titleData = data.name
    },

    summaryClose() {
      this.allContent = null
      this.handleState = false
    },
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    }
  }
}
</script>

<style lang="scss" scoped>
// ::v-deep .eladmin-editor{
//   width: 270px;
// }
</style>
